#include <stdio.h>
#include "ohos_init.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "cmsis_os2.h"

#define LED_GPIO1    7
#define LED_GPIO2    8

void ledGpioInit(int LED_GPIO)
{
    // 初始化LED引脚
    int ret = IoTGpioInit(LED_GPIO);
    if(ret != 0){
        printf("IoTGpioInit failed :%#x \r\n",ret);
        return;
    }
    // 设置复用为GPIO功能
    ret = IoTGpioSetFunc(LED_GPIO,IOT_GPIO_FUNC_GPIO_7_GPIO);
    if(ret != 0){
        printf("IoTGpioSetFunc failed :%#x \r\n",ret);
        return;
    }
    //设置方向为输出
    ret = IoTGpioSetDir(LED_GPIO,IOT_GPIO_DIR_OUT);
    if(ret != 0){
        printf("IoTGpioSetDir failed :%#x \r\n",ret);
        return;
    }
    printf("----- LedGpioInit success! -----\r\n");
}

void GPIOout1(void)
{
    ledGpioInit(LED_GPIO1);
    int val =1;
    IoTGpioSetOutputVal(LED_GPIO1,val);
}

void GPIOout2(void)
{
    ledGpioInit(LED_GPIO2);
    int val =0;
    while(1){
        
        if(val ==0)
        {
            val =1;
            IoTGpioSetOutputVal(LED_GPIO2,val);
        }
        else
        {
            val =0;
            IoTGpioSetOutputVal(LED_GPIO2,val);
        }
        osDelay(100);
    }
}

void GPIO_outdemo()
{
    osThreadAttr_t attr1;
    attr1.name = "GPIO_Led1";
    attr1.attr_bits = 0U;
    attr1.cb_mem = NULL;
    attr1.cb_size = 0U;
    attr1.stack_mem = NULL;
    attr1.stack_size = 1024 * 4;
    attr1.priority = 25;

    osThreadId_t threadID1 = osThreadNew((osThreadFunc_t)GPIOout1,NULL,&attr1);
    if(threadID1 == NULL)
    {
        printf("Falied to create GpioOutTask1!\r\n");
    }
    printf("create GpioOutTask1 OK!\r\n");

    osThreadAttr_t attr2;
    attr2.name = "GPIO_Led2";
    attr2.attr_bits = 0U;
    attr2.cb_mem = NULL;
    attr2.cb_size = 0U;
    attr2.stack_mem = NULL;
    attr2.stack_size = 1024 * 4;
    attr2.priority = 25;

    osThreadId_t threadID2 = osThreadNew((osThreadFunc_t)GPIOout2,NULL,&attr2);
    if(threadID2 == NULL)
    {
        printf("Falied to create GpioOutTask2!\r\n");
    }
    printf("create GpioOutTask2 OK!\r\n");
}

APP_FEATURE_INIT(GPIO_outdemo);